package com.czxy.advert.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.advert.feign.InfoFeign;
import com.czxy.advert.service.AdvertService;
import com.czxy.advert.utils.OssUtils;
import com.czxy.domain.TbAdvert;
import com.czxy.domain.TbPhoto;
import com.czxy.domain.TbRecruitmentInfo;
import com.czxy.domain.TbUser;
import com.czxy.findjob.vo.BaseResult;
import com.czxy.vo.AdvertVo;
import com.czxy.vo.InfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 广告(轮播图模块) 前端控制器
 * </p>
 *
 * @author xuewen
 * @since 2022-01-23
 */
@RestController
@RequestMapping("/advert")
public class AdvertController {

    @Autowired
    private AdvertService advertService;

    @Autowired
    private InfoFeign infoFeign;

    @Autowired
    private OssUtils ossUtils;

    @GetMapping("/{current}/{pageSize}/{isShow}")
    public BaseResult condition(@PathVariable("current") Integer current,@PathVariable("pageSize") Integer pageSize,@PathVariable("isShow") Integer isShow){
        Page<TbAdvert> page = advertService.condition(current,pageSize,isShow);
        return BaseResult.ok("查询成功",page);
    }
    @PostMapping("/adminFindAll")
    public BaseResult findAll(@RequestBody AdvertVo advertVo){
        Page<TbAdvert> condition = advertService.adminFindAll(advertVo);
        for (TbAdvert record : condition.getRecords()) {
            TbRecruitmentInfo data = infoFeign.findById(record.getRecruitmentId() + "").getData();
            record.setTbRecruitmentInfo(data);
        }
        return BaseResult.ok("查询成功",condition);
    }
    @DeleteMapping("/delByInfoId/{id}")
    public BaseResult delByInfoId(@PathVariable("id") Integer id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("recruitment_id",id);
        List<TbAdvert> list = advertService.list(queryWrapper);
        if (list.size()>0){
            List<Integer> collect = list.stream().map(tbAdvert -> tbAdvert.getId()).collect(Collectors.toList());
            advertService.removeByIds(collect);
        }
        return BaseResult.ok("删除成功");
    }
    @DeleteMapping("/delById/{id}")
    public BaseResult delById(@PathVariable("id") Integer id){
        boolean b = advertService.removeById(id);
        if (b){
            return BaseResult.ok("删除成功");
        }else {
            return BaseResult.ok("删除失败");
        }
    }
    @PostMapping("/upload")
    public BaseResult upload(@RequestParam("file") MultipartFile file){
        String url = ossUtils.upload(file);
        System.out.println(url);
        return BaseResult.ok("上传成功",url);
    }
    @PostMapping("/saveOrUpdate")
    public BaseResult saveOrUpdate(@RequestBody TbAdvert tbAdvert){
        TbRecruitmentInfo data = infoFeign.findById(tbAdvert.getRecruitmentId() + "").getData();
        if (data==null){
            return BaseResult.error("没有当前岗位("+tbAdvert.getRecruitmentId()+")");
        }
        if (tbAdvert.getId()==null){
            tbAdvert.setIsShow(1);
        }
        boolean b = advertService.saveOrUpdate(tbAdvert);
        if (b){
            return BaseResult.ok("保存成功");
        }else {
            return BaseResult.error("保存失败");
        }
    }
    @GetMapping("/findById/{id}")
    public BaseResult findById(@PathVariable("id") Integer id){
        TbAdvert tbAdvert = advertService.getById(id);
        tbAdvert.setTbRecruitmentInfo(infoFeign.findById(tbAdvert.getRecruitmentId()+"").getData());
        return BaseResult.ok("查询成功",tbAdvert);
    }

}

